package com.yk.faceapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.HttpDownloader;
import com.yk.faceapplication.util.LogDebugger;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/16_10:58
 */

public class AuthorizeActivity extends BaseActivity{
    private TextView nameTV,mobileTV;
    private Button commitBtn;
    private String url;
    private TextView registerTV, manageTV;

    private ImageView agreeIV;
    private LinearLayout agreeLayout;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("开通鑫e贷");

        agreeIV = (ImageView) findViewById(R.id.agree_img);
        agreeLayout = (LinearLayout) findViewById(R.id.agree_layout);
        agreeLayout.setOnClickListener(this);


        nameTV = (TextView) findViewById(R.id.member_name_tv);
        mobileTV = (TextView) findViewById(R.id.member_mobile_tv);
        mobileTV.setText(mApplication.getMember().getMobile());
        commitBtn = (Button) findViewById(R.id.commit_btn);
        commitBtn.setOnClickListener(this);

        registerTV = (TextView) findViewById(R.id.register_tv);
        manageTV = (TextView) findViewById(R.id.manage_tv);
        registerTV.setOnClickListener(this);
        manageTV.setOnClickListener(this);

        checkInputInfo();
//        getUrl();
    }

    class downloadFileThread extends Thread{
        public void run(){
            HttpDownloader httpDownloader=new HttpDownloader();
            String fileData=httpDownloader.downloadFiles(url);
            LogDebugger.info(fileData);
        }
    }

    private void getUrl() {


        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                try {
                    url = jsonObject.getString("data");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.getRequest(this, RequestUrl.GET_REGISTER_URL, callback);
    }

    private void checkInputInfo(){
        if(agreeIV.getVisibility() == View.INVISIBLE){
            commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
            commitBtn.setEnabled(false);
            commitBtn.setTextColor(getResources().getColor(R.color.textBlack));
        }else{
            commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
            commitBtn.setEnabled(true);
            commitBtn.setTextColor(getResources().getColor(R.color.white));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.commit_btn:
                authorize();
                break;
            case R.id.register_tv:
            case R.id.manage_tv:
//                if (url == null) {
                ////                    return;
                ////                }
                Intent intent = new Intent(AuthorizeActivity.this,AgreeWebviewActivity.class);
//                intent.putExtra("url","file:///android_asset/xed-agreement.htm");
                startActivity(intent);
                break;
            case R.id.agree_layout:
                if(agreeIV.getVisibility() == View.VISIBLE){
                    agreeIV.setVisibility(View.INVISIBLE);
                }else{
                    agreeIV.setVisibility(View.VISIBLE);
                }
                checkInputInfo();
                break;
        }
    }

    private void authorize(){
        Map<String, String> form = new HashMap<String, String>();
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                Toast.makeText(mApplication, "开通成功", Toast.LENGTH_SHORT).show();
                authorizePoint();
                setResult(RESULT_OK);
                finishAct();
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.request(this, RequestUrl.OPEN+"token="+mToken,callback,form);
    }

    private void authorizePoint(){
        Map<String, String> form = new HashMap<String, String>();
        form.put("userAgentCode","2");
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.request(this, RequestUrl.OPEN_POINT+"token="+mToken,callback,form);
    }


}
